package settingsapp.settings.continuous;

import settingsapp.settings.ClickableSettingChanger;
import settingsapp.ui.ContinuousChangerActivity;
import settingsapp.ui.PanelGridActivity;
import android.content.Intent;

public abstract class ContinuousSettingChanger implements ClickableSettingChanger
{
	public static final String ONSET_INITIAL_VALUE = "initial_value";
	public static final String ONSET_RESULT_VALUE = "result_value";
	public static final int CONTINUOUS_SETTING_REQUEST_CODE = 0;
	
	protected PanelGridActivity context;
	protected int absoluteValue;
	private int intRepresentation = -1;
	
	@Override
	public String toString()
	{
		return "1." + intRepresentation;
	}

	/**
	 * Set these to the boundaries for a given setting's 
	 * absolute value (used to calculate the percentage)
	 */
	protected int max, min;
	protected String acticvityTag;
	/**
	 * Used to determine which implementation to use to handle
	 * onActivityResult
	 */
	protected int settingCode;
	
	public ContinuousSettingChanger(PanelGridActivity newContext, int intRepresentation)
	{
		context = newContext;
		this.intRepresentation = intRepresentation;
	}
	
	protected int getPercentage(int currentValue)
	{
		return Math.round((currentValue * 100 ) / ((float)(max - min)));
	}
	
	protected int getIntFromPercentage(int currentPercentage)
	{
		return min + Math.round((currentPercentage / 100f) * (max - min));
	}
	
	protected void setValue(boolean value)
	{
		// do nothing
		// this is not handled in continuous settings, only toggle settings
	}
	abstract protected void setIntValue(int value);

	/**
	 * starts activity based on the string for this setting
	 * @return the value after toggle is called
	 */
	public void startSetValueActivity()
	{
		// TODO start activity based on the string for this setting
		// 1) Call getValue()
		// 2) Pass in to activity with startActivityForResult(...)
		context.setChangerCallback(this);
		Intent intent = new Intent(context, ContinuousChangerActivity.class);
		intent.putExtra(ONSET_INITIAL_VALUE, getPercentage());
		context.startActivityForResult(intent, 0);
	}
	
	protected int getPercentage()
	{
		readValue();
		return getPercentage(absoluteValue);
		
	}
	
	public boolean clickAction()
	{
		startSetValueActivity();
		return true;
	}
	
	/**
	 * Handles modifying the setting. Call this in
	 * on Activity result
	 * @param newPercentage the percentage that the setting was set
	 * to in the Activity
	 */
	public void handleSettingChange(int newPercentage)
	{
		setIntValue(getIntFromPercentage(newPercentage));
	}
	
	/**
	 * Stub method, always returns true
	 */
	public boolean getValue()
	{
		return true;
	}
	
	/**
	 * Sets value from system settings
	 * (must be called before returning value
	 * to be correct)
	 */
	abstract protected void readValue();
	
	/**
	 * retrieve the icon resource id for this setting
	 */
	abstract public int getIconResource();
	
	// an continuous settings icon is the same no matter what (no on or off)
	public int getResourceOn()
	{
		return getIconResource();
	}
	
	public int getResourceOff()
	{
		return getIconResource();
	}
}
